/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.sanselan.common.byteSources.ByteSource;
import org.apache.sanselan.common.byteSources.ByteSourceFile;
import org.apache.sanselan.formats.jpeg.JpegImageParser;
import org.apache.sanselan.formats.jpeg.JpegUtils;
import org.apache.sanselan.formats.jpeg.xmp.JpegXmpRewriter;
import org.apache.sanselan.formats.jpeg.xmp.JpegRewriter;
import org.apache.sanselan.util.Debug;

public class JpegXmpRewriteTest extends JpegRewriter {

    public void testRemoveInsertUpdate()  throws Exception{
      
            final File imageFile = new File("1.jpg");
            Debug.debug("imageFile", imageFile);          

            final ByteSource byteSource = new ByteSourceFile(imageFile);
            Debug.debug("Source Segments:");
            new JpegUtils().dumpJFIF(byteSource);

            final Map<String,Object> params = new HashMap<String,Object>();
            final String xmpXml = new JpegImageParser().getXmpXml(byteSource, params);

            Debug.debug("xmpXml", xmpXml.length());
            Debug.debug(xmpXml);            
          
            final File noXmpFile = new File("NoXMP" + imageFile.getName());
            {
                // Test remove            	
                OutputStream os = null;
                try {
                    os = new FileOutputStream(noXmpFile);
                    os = new BufferedOutputStream(os);
                    new JpegXmpRewriter().removeXmpXml(byteSource, os);
                } finally {
                    if (os != null) {
                        os.close();
                    }
                    os = null;
                }

                 Debug.debug("Source Segments:");
                 new JpegUtils().dumpJFIF(new ByteSourceFile(noXmpFile));

                final String outXmp = new JpegImageParser().getXmpXml(new ByteSourceFile(noXmpFile), params);
                System.out.println(outXmp);
            }

            {
                // test update
                final String newXmpXml = "test";
                final File updated = new File("Updated" + imageFile.getName());
                OutputStream os = null;
                try {
                    os = new FileOutputStream(updated);
                    os = new BufferedOutputStream(os);
                    
                /*    JFIFPieces jfifPieces = analyzeJFIF(byteSource);
            		List pieces = jfifPieces.pieces;
            		pieces = removeXmpSegments(pieces);
            		
            		for(Object o : pieces)
            			System.out.println("Pieces " + o );*/
            		
                    new JpegXmpRewriter().updateXmpXml(byteSource, os,newXmpXml);
                } finally {
                    if (os != null) {
                        os.close();
                    }
                    os = null;
                }

                Debug.debug("Source Segments:");
                new JpegUtils().dumpJFIF(new ByteSourceFile(updated));

                final String outXmp = new JpegImageParser().getXmpXml(new ByteSourceFile(updated), params);
                System.out.println(outXmp);
            }

            {
                // test insert            	
                final String newXmpXml = "test";
                final File updated = new File("insert" + imageFile.getName());
                OutputStream os = null;
                try {
                    os = new FileOutputStream(updated);
                    os = new BufferedOutputStream(os);
                    new JpegXmpRewriter().updateXmpXml(new ByteSourceFile(noXmpFile), os, newXmpXml);// insert XMP to a non XMP file
                } finally {
                    if (os != null) {
                        os.close();
                    }
                    os = null;
                }

                 Debug.debug("Source Segments:");
                 new JpegUtils().dumpJFIF(new ByteSourceFile(updated));

                final String outXmp = new JpegImageParser().getXmpXml(new ByteSourceFile(updated), params);
                System.out.println(outXmp);
            }
            	
    }

}
